package com.zzyl.nursing.controller;

import com.huaweicloud.sdk.iotda.v5.model.ServiceCapability;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.DevicePropertiesVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2025-09-26
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 点击同步数据按钮--数据放到redis 中
     */
    @PostMapping("/syncProductList")
    public AjaxResult syncProductList(){
        deviceService.syncProductList();
     return success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    public AjaxResult allProduct(){
        List<ProductVo> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 新增设备
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody DeviceDto deviceDto){
        deviceService.register(deviceDto);
        return success();
    }
    /**
     * 查询设备详细数据
     */
    @GetMapping("/{iotId}")
    public AjaxResult iotDetail(@PathVariable String iotId){
        DeviceDetailVo device = deviceService.iotDetail(iotId);
        return success(device);
    }


    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    public AjaxResult queryServiceProperties(@PathVariable String iotId){
        List<DevicePropertiesVo>  devicePropertiesVo = deviceService.queryServiceProperties(iotId);
        return success(devicePropertiesVo);
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/queryProduct/{productKey}")
    public AjaxResult queryProduct(@PathVariable String productKey){
        List<ServiceCapability> device =deviceService.queryProduct(productKey);
        return success(device);
    }


}
